﻿using System;
using System.Collections.Generic;
using System.Web;
using OSGeo.MapGuide;
namespace IGIS.Extensions.markup
{
    public class MarkupSchemaFactory
    {
        public static MgDataPropertyDefinition CreateIDProperty()
        {
            MgDataPropertyDefinition idProperty = new MgDataPropertyDefinition("ID");
            idProperty.SetDataType(MgPropertyType.Int32);
            idProperty.SetReadOnly(true);
            idProperty.SetNullable(false);
            idProperty.SetAutoGeneration(true);

            return idProperty;
        }
        public static MgDataPropertyDefinition CreateIntProperty(string name, bool autoGeneration)
        {
            MgDataPropertyDefinition idProperty = new MgDataPropertyDefinition(name);
            idProperty.SetDataType(MgPropertyType.Int32);
            idProperty.SetReadOnly(true);
            idProperty.SetNullable(false);
            idProperty.SetAutoGeneration(autoGeneration);

            return idProperty;
        }

        public static MgDataPropertyDefinition CreateTextProperty(int length)
        {
            MgDataPropertyDefinition textProperty = new MgDataPropertyDefinition("Text");
            textProperty.SetDataType(MgPropertyType.String);
            textProperty.SetLength(length);

            return textProperty;
        }

        public static MgDataPropertyDefinition CreateReverseTextProperty(int length)
        {
            MgDataPropertyDefinition textProperty = new MgDataPropertyDefinition("ReverseText");
            textProperty.SetDataType(MgPropertyType.String);
            textProperty.SetLength(length);

            return textProperty;
        }

        public static MgGeometricPropertyDefinition CreateGeometryProperty(int geomType)
        {
            MgGeometricPropertyDefinition geometryProperty = new MgGeometricPropertyDefinition("Geometry");
            geometryProperty.SetGeometryTypes(geomType);
            geometryProperty.SetHasElevation(false);
            geometryProperty.SetHasMeasure(false);
            geometryProperty.SetReadOnly(false);
            geometryProperty.SetSpatialContextAssociation("LL84");

            return geometryProperty;
        }

        public static MgClassDefinition CreateMarkupClass()
        {
            MgClassDefinition markupClass = new MgClassDefinition();
            markupClass.SetName("Markup");
            MgPropertyDefinitionCollection properties = markupClass.GetProperties();
            MgDataPropertyDefinition idProperty = MarkupSchemaFactory.CreateIDProperty();
            properties.Add(idProperty);
            properties.Add(MarkupSchemaFactory.CreateIntProperty("UID", true));
            properties.Add(MarkupSchemaFactory.CreateTextProperty(255));
            properties.Add(MarkupSchemaFactory.CreateReverseTextProperty(255));
            properties.Add(MarkupSchemaFactory.CreateIntProperty("ProjectId", false)); //Need for ProjectManager.
            properties.Add(MarkupSchemaFactory.CreateGeometryProperty(MgFeatureGeometricType.Point | MgFeatureGeometricType.Curve | MgFeatureGeometricType.Surface));

            markupClass.GetIdentityProperties().Add(idProperty);
            markupClass.SetDefaultGeometryPropertyName("Geometry");

            return markupClass;
        }

        public static MgFeatureSchema CreateMarkupSchema()
        {
            MgFeatureSchema markupSchema = new MgFeatureSchema();
            markupSchema.SetName("MarkupSchema");

            markupSchema.GetClasses().Add(MarkupSchemaFactory.CreateMarkupClass());

            return markupSchema;
        }
    }
}